package com.deyuanyun.pic.pipe.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.util.propertyeditor.DateEditor;
import com.deyuanyun.pic.common.util.propertyeditor.LongEditor;
import com.deyuanyun.pic.settings.domain.pipe.PipeLine;
import com.deyuanyun.pic.settings.domain.pipe.PipeSegment;
import com.deyuanyun.pic.settings.service.pipe.PipeLineService;
import com.deyuanyun.pic.settings.service.pipe.PipeSegmentService;

/**
 * Created by linwei on 2015/8/4.
 */
public class BaseController {

    /**
     * 日志
     */
    protected Logger logger = LogManager.getLogger(this.getClass());

    @Resource
    private PipeLineService pipeLineService;

    @Resource
    private PipeSegmentService pipeSegmentService;


    @ModelAttribute( "initSomeParmMap" )
    public ModelMap initSomeParm(){
        ModelMap map=new ModelMap();

        return map;
    }

    /**
     * 初始化时设置一些参数的转换
     *
     * @param binder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        /*binder.registerCustomEditor(Date.class, new CustomDateEditor(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));*/
        binder.registerCustomEditor(Date.class, new DateEditor());

        binder.registerCustomEditor(int.class,new LongEditor());
    }

    
    /**
     * 得到日志
     *
     * @return
     */
    public Logger getLogger() {
        return logger;
    }

    /**
     * 路径编码器
     * @param value java.long.String 传入值
     * @return java.long.String 返回编码后的字符串
     */
    protected String urlEncoder(String value) {
        try {
            return URLEncoder.encode(value, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return value;
        }
    }

    /**
     * 路径解码器
     *
     * @param value java.long.String 传入值
     * @return java.long.String 返回解码后的字符串
     */
    protected String urlDecoder(String value) {
        try {
            return URLDecoder.decode(value, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return value;
        }
    }

    /**
     * 得到管道
     *
     * @return
     */
    protected List<PipeLine> getPipeLine(){
        return pipeLineService.getLine();
    }

    /**
     * 根据管道id 得到管段
     * @param lineId
     * @return
     */
    protected List<PipeSegment> getPipeSegment(String lineId){
        return pipeSegmentService.getSegmentByLineId(lineId);
    }


    /**
     * 所有的forword action都由这里转发
     *
     * @param viewName
     * @param context
     * @return
     */
    public ModelAndView forword(String viewName, Map context){
        /*ModelAndView mv = new ModelAndView();
        //添加模型数据 可以是任意的POJO对象
        mv.addObject("message", "Hello World!");
        //设置逻辑视图名，视图解析器会根据该名字解析到具体的视图页面
        mv.setViewName("hello");*/
        return new ModelAndView(viewName,context);
    }

    /**
     *
     * @param viewName
     * @return
     */
    public ModelAndView forword(String viewName){
        return forword(viewName,null);
    }

    public ModelAndView redirect(String url){
        return new ModelAndView("redirect:"+url);
        /*如果需要addFlashAttribute传参数的话
        @RequestMapping(value="addcustomer", method=RequestMethod.POST)
        public String addCustomer(@ModelAttribute("customer") Customer customer,
        final RedirectAttributes redirectAttributes) {
            redirectAttributes.addFlashAttribute("customer", customer);
            redirectAttributes.addFlashAttribute("message","Added successfully.");
            return "redirect:showcustomer.html";
        }*/
    }
}